package com.eminem.service.impl;

import com.eminem.dao.UserDao;
import com.eminem.dao.UserFocusDao;
import com.eminem.dao.UserInfoDao;
import com.eminem.model.User;
import com.eminem.model.UserFocus;
import com.eminem.model.UserInfo;
import com.eminem.service.UserService;
import com.eminem.utils.MailUtil;
import com.eminem.utils.StrUtil;

import javax.mail.MessagingException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class UserServletimpl implements UserService {

    private UserDao userDao = new UserDao();

    private UserInfoDao userInfoDao = new UserInfoDao();

    private UserFocusDao userFocusDao = new UserFocusDao();

    @Override
    public User Usercheck(String username) {
        List<User> list = userDao.findAll("username",username);
        if (list.isEmpty()){
            return null;
        }else {
            return list.get(0);
        }
    }

    @Override
    public User findEmail(String email) {
        List<User> list = userDao.findAll("email", email);
        if (list.isEmpty()){
            return null;
        }else {
            return list.get(0);
        }
    }

    @Override
    public boolean cheackNameorEmail(String email, String username) {
        Map<String, Object> map = new HashMap<>();
        map.put("username",username);
        map.put("email",email);
        List<User> list = userDao.findByCols("or", map);
        return list.isEmpty();
    }

    @Override
    public boolean addUser(User user) {
        if (userDao.save(user)> 0 ){
            Map<String, Object> map = userDao.findUserBaseInfo(user.getUsername());
            UserInfo info = new UserInfo();
            info.setUserId(Integer.parseInt(map.get("userId").toString()));
            info.setRealName("菜鸟教程");
            info.setRegisterTime(new Date());
            return userInfoDao.save(info) > 0;
        }
        return false;

    }

    @Override
    public String sendEmailCode(String email) {
        String code = StrUtil.getRandCode(4);
        String title = "《小标交友》 重置密码";
        String content = "您的验证码是： "+code+", 两分钟之内有效。";
        try {
            MailUtil.send(email,title,content,MailUtil.UNICODE);
        } catch (MessagingException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return code;
    }

    @Override
    public boolean resetPassword(String email, String passwoed) {
        List<User> users = userDao.findAll("email", email);
        if (!users.isEmpty()){
            User user = users.get(0);
            user.setPassword(passwoed);
            return userDao.update(user) > 0;
        }
        return false;
    }

    @Override
    public Map<String, Object> findUserInfo(String username) {
        Map<String, Object> map = userDao.findUserBaseInfo(username);
        if (map.isEmpty()){
            return null;
        }
        return map;
    }

    @Override
    public Map<String, Object> getUserInfo(String id) {
        return userDao.getUserInfo(id);
    }

    @Override
    public void updateLoginTime(Integer id) {
        List<UserInfo> byId = userInfoDao.findById(id);
        if (byId != null){
            UserInfo userInfo = byId.get(0);
            Date date = new Date();
            userInfo.setLoginTime(date);
            userInfoDao.update(userInfo);
        }

    }

    @Override
    public void updateRegisterTime(Integer id) {
        List<UserInfo> byId = userInfoDao.findById(id);
        if (byId != null){
            UserInfo userInfo = byId.get(0);
            Date date = new Date();
            userInfo.setLoginTime(date);
            userInfoDao.save(userInfo);
        }
    }

    @Override
    public List<Map<String, Object>> getUserList(String userId, int currentPage, String names) {
        return userDao.findUserList(userId,currentPage,names);
    }


    @Override
    public UserFocus getFollowUser(String userId, String ufId) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id",userId);
        map.put("user_focus_id",ufId);
        List<UserFocus> list = userFocusDao.findByCols("and", map);
        if (!list.isEmpty()){
            return list.get(0);
        }
        return null;
    }

    @Override
    public boolean followUser(String userId, String ufId) {
        UserFocus focus = new UserFocus();
        focus.setUserId(Integer.parseInt(userId));
        focus.setUserFocusId(Integer.parseInt(ufId));
        return userFocusDao.save(focus)>0;
    }

    @Override
    public long getUserCount(String name) {
        return userDao.getUserTotal(name);
    }

    @Override
    public boolean unfollowUser(Integer id) {
        return userFocusDao.deleteById(id)>0;
    }

    @Override
    public void uploadImg(String userId, String s) {
        List<UserInfo> list = userInfoDao.findById(userId);
        UserInfo info = list.get(0);
        info.setPic(s);
        userInfoDao.update(info);
    }

    @Override
    public List<Map<String, Object>> getUserName(String userId,int currentPage) {
        return userDao.findUserName(userId,currentPage);
    }

    @Override
    public long getFollowCount(String userId) {
        return userDao.getFollowCount(userId);
    }
}
